<?php

class Member_AddressController extends Zend_Controller_Action
{

	private $request;
	private $common;
	private $db;
    public function init()
    {
		$this->request = $this->getRequest();

		include_once "../library/inc/Zyt_Common.php"; 
		$this->common = Zyt_Common::getInstance();
		
		$this->common->checklogin(true, true);

		include_once "../library/inc/Db_Config.php";
		$this->db = Db_Config::getInstance()->Member();
        /* Initialize action controller here */
    }

	public function getlistAction()
	{
		$memberid = $this->request->getParam("MemberID");
		$where = "";
		if($memberid > 0) {
			$where = "WHERE memberid = $memberid";
		}
		
		$_iAmount = $this->db->fetchOne("SELECT count(*) FROM address $where");
		$result = $this->db->fetchAll("SELECT id, realname, firstregion,  secondregion, thirdregion, detail, zipcode, tel, mobile, isdefault FROM address $where");
		$sMsg = "";
		$len = count($result);
		for($i=0; $i < $len; $i++) {
			$comma = ",";
			if($i == ($len -1)) 
			{
				$comma = "";
			}
			$sMsg .= "{id:" . $result[$i]["id"] . ",realname:'" . $result[$i]["realname"] . "',firstregion:'" . $result[$i]["firstregion"] . "',secondregion:'" . $result[$i]["secondregion"] . "', thirdregion:'" . $result[$i]["thirdregion"] . "',detail:'" . $result[$i]["detail"] . "',zipcode:'" . $result[$i]['zipcode'] . "',tel:'" . $result[$i]["tel"] . "',mobile:'" . $result[$i]["mobile"] . "',isdefault:'" . $result[$i]["isdefault"] . "'}" . $comma;
		}

		$acl = array("aRead" => 1, "aNew" => 1, "aEdit" => 1, "aLock" => 1, "aDel" => 1, "aList" => 1);
		$_sExtJSD = "Acl:[{aRead:" . $acl["aRead"] . ", aNew:" . $acl["aNew"] . ",aEdit:" . $acl["aEdit"] . ",aDel:" . $acl["aDel"] .
							  ",aList:" . $acl["aList"] . "}]";
		$this->common->PutData('{totalProperty:' . $_iAmount . ',records:[' . $sMsg . '],' . $_sExtJSD . '}');
		exit;
	}

	public function delAction()
	{
		$uids = $this->request->getParam("aid");
		
		if($this->db->update("member", array(
					"status" => 1
				), $this->db->quoteInto('memberid in (?)', $uids))) 
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}

	public function lockAction()
	{
		$uids = $this->request->getParam("aid");
		
		$stmt = $this->db->query("UPDATE member SET status = 2 - status & 2 WHERE status in (0,2) AND " . $this->db->quoteInto('memberid in (?)', $uids));
		if($stmt->rowCount())
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}
}